package xj.graph2d.app.project;

import static xj.app.BaseApplicationFrame.getResourceString;

import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Date;

import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;

import xj.graph2d.app.DrawAction;
import xj.graph2d.dialog.DialogPanel;
import xj.graph2d.dialog.TitledDialogPanel;

public class ProjectRenameListener 
  extends DrawAction 
  implements ActionListener {

  class ProjectRenameOptionPanel extends DialogPanel {

    public ProjectRenameOptionPanel(String name) {
      super(name);
      JLabel nameLabel = new JLabel(getResourceString("ProjectName.text") + " ");
      nameField = new JTextField();

      JLabel typeLabel = new JLabel(getResourceString("ProjectType.text") + " ");
      typeField = new JComboBox();
      typeField.addItem(ProjectType.UML);
      typeField.addItem(ProjectType.Default);
      typeField.setEnabled(false); 

      JLabel ctimeLabel1 = new JLabel(getResourceString("Create.text") + " ");
      ctimeLabel = new JLabel();

      addRow(nameLabel, nameField);
      addRow(typeLabel, typeField);
      addRow(ctimeLabel1, ctimeLabel);

      setPreferredSize(new Dimension(400, 150));
      setOption();
    }

    @Override
    public void setOption() {
      ProjectManager projectManager = main.getProjectManager();
      Project project = projectManager.getCurrentProject();
      String name = project.getName();
      ProjectType type = project.getProjectType();
      nameField.setText(name);
      typeField.setSelectedItem(type);
      ctimeLabel.setText((new Date(project.getCreationTime())).toString());
    }

    @Override
    public void applyOption() {
      ProjectManager projectManager = main.getProjectManager();
      Project project = projectManager.getCurrentProject();
      String name = nameField.getText();
      if (!project.getName().equals(name)) {  
	project.setName(name);
      }
    }

    protected JTextField nameField;

    protected JComboBox typeField;

    protected JLabel ctimeLabel;

  }

  protected TitledDialogPanel projectRenameOptionDialog = null;
  protected ProjectRenameOptionPanel projectRenamePanel = null;
  
  protected String projectRenamePanelTitle;
  protected String projectRenameDialogTitle;
  protected String projectRenameHeader;
  protected String[] projectRenameText;
  
  @Override
  protected void init() { 
    projectRenamePanelTitle = getResourceString("ProjectRename.title");
    projectRenameDialogTitle = getResourceString("ProjectRenameDialog.title");
    projectRenameHeader = getResourceString("ProjectRenameDialog.header");
    projectRenameText = new String[] { 
	getResourceString("ProjectRenameDialog.text1"),
	getResourceString("ProjectRenameDialog.text2"),	
    };
  }
  
  public void actionPerformed(ActionEvent e) {
    if (projectRenameOptionDialog == null) {
      DialogPanel[] options = { 
	projectRenamePanel = new ProjectRenameOptionPanel(projectRenamePanelTitle), 
      };
      projectRenameOptionDialog = new TitledDialogPanel(main.getJFrame(), 
							JOptionPane.OK_CANCEL_OPTION, 
							null, options);
      projectRenameOptionDialog.setTitle(projectRenameDialogTitle);
      projectRenameOptionDialog.setTitlePaneText(projectRenameHeader, projectRenameText);
    }
    projectRenameOptionDialog.setOptions();
    projectRenameOptionDialog.showOptionDialog();
  }

}
